﻿/*
 * NetXtcpSystem.h
 */
#ifndef __GDK_XNETTCPSYSTEM_H__
#define __GDK_XNETTCPSYSTEM_H__

#include <gdk/net/Socket.h>
#include <gdk/net/NetXtcpServer.h>
#include <gdk/net/NetXtcpConnection.h>
#include <gdk/net/NetMessage.h>
#include <gdk/net/NetSystem.h>


namespace gdk
{


	///////////////////////////////////////////////
	class GDK_NET_EXPORT NetXtcpSystem : public NetSystem
	{
		DECLARE_CLASS_HANDLE(NetXtcpSystem)
		DECLARE_CLASS_INSTANCE1(NetXtcpSystem, uint16_t)

	private:
		NetXtcpServerPtr	_xtcpServer;
		uint16_t			_port;

	private:
		NetXtcpSystem(uint16_t port);
		~NetXtcpSystem(void);

	public:
		virtual bool init(void);
		virtual void exit(void);
		//-----------------------------------------------------------------

	public:
		__INLINE__ uint16_t getPort(void) const { return _port; }
		//-----------------------------------------------------------------

	private:
		bool xtcp_start(NetXtcpConnection *conn);
		void xtcp_stop(NetXtcpConnection *conn);
	private:
		NetXtcpConnectionPtr createXtcpConnection(void);
		//-----------------------------------------------------------------
		void onAccept(const Evt_NetXtcpAccept *evt);
		void onConnectOk(const Evt_NetXtcpConnectOk *evt);
		void onConnectFail(const Evt_NetXtcpConnectFail *evt);
		void onBlocking(const Evt_NetXtcpBlocking *evt);
		void onRestored(const Evt_NetXtcpRestored *evt);
		void onClosed(const Evt_NetXtcpClosed *evt);
		void onError(const Evt_NetXtcpError *evt);
		void onAbort(const Evt_NetXtcpAbort *evt);
		void onReceive(const Evt_NetXtcpReceive *evt);
		//-----------------------------------------------------------------
		void onNetConnect(NetConnect *msg);
		void onNetClose(NetClose *msg);
		void onNetSend(NetSend *msg);
		//-----------------------------------------------------------------
	};
	typedef NetXtcpSystem::Handle NetXtcpSystemPtr;



} //namespace gdk



#endif //__GDK_XNETTCPSYSTEM_H__
